<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Session;
class UserTxController extends Controller
{
    
    public function update(Request $request){
		//查询数据根据session
		$session=$request->session()->get('id');
		//查询数据库
		$result=DB::table('userdetail')->where('did','=',1)->first();

		//分配数据
        return view('user.updateTx',['result'=>$result]);
    }
	
	
	//上传图片
	public function upload(Request $request){
		//接收值
		$result1=$request->only('dphoto');
		
		//dd($result1);
		//$result2=$request->only('did');
		$result3=$_FILES['dphoto']['name'];
		//上传图片
		if($request->hasFile('dphoto')){
			//文件路径
			$dir='uploads/';
			//获取后缀名
			$result=$request->file('dphoto')->getclientOriginalExtension();
			//上传文件名
			$fname=uniqid().'.'.$result;
		
			//设置文件路径
			$result1['dphoto']=$dir.$fname;
			//dd($goodsInfo);
			//移动图片
			$request->file('dphoto')->move($dir,$fname);
		}else{
			//上传失败继续回添加页面
			//跳转商品添加页面
			
		}
		
		$res=DB::table('userdetail')->where('did','=',1)->update($result1);
		if($res){
			return redirect('/userTx');
		}else{
			return redirect('/upload');
		}
	}
	
	//地址管理
	public function addAdress(Request $request){
		//查询数据根据session
		$session=$request->session()->get('id');
		//查询数据库
		$result2=DB::table('getaddr')->where('uid','=',1)->get();
		$result3=DB::table('userdetail')->where('did','=',1)->first();
	
		//分配数据
        return view('user.addressGl',['result1'=>$result2,'result'=>$result3]);
	}
	
	
	//地址管理操作
	public function address(Request $request){
		//接收表单传递过来的值
		$result=$request->only('uid','postalcode','address','phone','name','province','city','county');
		
		$result['uid']=1;
		//插入数据
		$res = DB::table('getaddr')->insert($result);
		if($res){
			return redirect('/addressGl');
		}else{
			return redirect('/addressGl');
		}
	}
	
	
	//地址管理删除
	public function deleteAddress($id){
		//删除数据
		$result=DB::table('getaddr')->where('id','=',$id)->delete();
		if($result){
			return redirect('/addressGl');
		}else{
			return redirect('/addressGl');
		}
	}
	
	
	//地址管理修改
	public function updateAddress($id){
		$result=DB::table('getaddr')->where('id','=',$id)->first();
		if($result){
			return   json_encode($result);
		}
	}
	
	//默认地址设置
	public function setAddress($id,$status){
		$result=DB::select('select status from lamp_getaddr  having(count(status))>1');
		
		if($status){
			$result=DB::table('getaddr')->where('id','=',$id)->update(['status'=>0]);
		}else{
			$result=DB::table('getaddr')->where('id','=',$id)->update(['status'=>1]);
		}
		
		if($result){
			return redirect('/addressGl');
		}else{
			return redirect('/addressGl'); 
		}
	}
	
}
